## plotting arabic in R ## get unicode from pasted text or from here: ## http://unicode.org/Public/UNIDATA/ArabicShaping.txt ## just replace the first 0 with u x2 <- "\uFDF2" ## the unicode for allah x2 plot.new(); plot.window(0:1,0:1); text(0.5,0.5,x2,cex=12) ## This is great, but sometimes you don't want to enter ## the unicode as unicode. Perhaps you have the unicode numbers ## and you want to turn it into unicode somehow. Well, you can't ## do it directly because you can't do paste("\","u0631",sep="") ## and x <- paste("\\","u0631", sep="") plot(0,0,type="n") text(0,0,x) ## doesn't get you what you want ## Instead we use the Unicode package library(Unicode) x <- c("0x0631","0x0633","0x0648","0x0644") plot(0,0,type="n") x2 <- intToUtf8(x) text(0,0,x2) ## With multiple words, you can't just feed it all through at once. ## Instead, do this: ## This is just constructing some fake words word1 <- c("0x0647", "0x0644") word2 <- c("0x0646","0x0639") word3 <- c("0x0631","0x0645","0x0639") word4 <- c("0x062f","0x0645", "0x062d", "0x0627") ## This is what I do ## first, put it into a list words <- list(word1,word2,word3,word4) ## then apply the unicode function to the list, unlist, and collapse wordsToPlot <-paste(unlist(lapply(words,intToUtf8)),collapse=" ") plot(0,0,type="n") text(0,0,wordsToPlot) ## sometimes, arabic gets dumped into files and looks like this: x <- "" x ## Not very useful...we'd like to convert x back to unicode ## http://stackoverflow.com/questions/17761858/converting-a-u-escaped-unicode-string-to-ascii x1 <- paste(paste0("\\u",strsplit(gsub("<|>","",x), "U+",fixed=T)[[1]][-1]), collapse="") x2 <- parse(text = paste0("'", x1, "'")) x3 <- x2[[1]] x3